package com.atwisdom.star.core.dao.pojo.basicClass;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.core.dao.pojo.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 
 * @TableName config
 */
@TableName(value ="config",autoResultMap = true)
@Data
public class Config extends BaseEntity {
    /**
     * 配置编码
     */
    @TableId(value = "id")
    private String id;

    /**
     * 配置名称
     */
    @TableField(value = "name")
    private String name;

    /**
     * 配置内容
     */
    @TableField(value = "value",typeHandler = FastjsonTypeHandler.class)
    private JSONObject value;

    /**
     * 说明
     */
    @TableField(value = "comments")
    private String comments;

    /**
     * 逻辑删除
     */
    @TableField(value = "deleteFlag")
    private Boolean deleteFlag;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public Config(JSONObject jsonObject) {
        Set<String> keys = jsonObject.keySet();
        this.id = keys.contains("id") ? jsonObject.getString("id") : null;
        this.name = jsonObject.getString("name");
        this.value = keys.contains("value") ? jsonObject.getJSONObject("value") : null;
        this.comments = keys.contains("comments") ? jsonObject.getString("comments") : null;
    }

    /**
     * 获取摘要数据列
     *
     * @return 反回查询列
     */
    public static String[] fetchSimpleColumns() {
        List<String> columns = new ArrayList<>();
        columns.add("id");
        columns.add("name");
        columns.add("value");
        columns.add("comments");
        columns.add("updatedBy");
        columns.add("updateTime");
        columns.add("createdBy");
        columns.add("createTime");
        return columns.toArray(new String[0]);
    }

    public JSONObject fetchJsonObject() {
        JSONObject JsonObject = new JSONObject();
        JsonObject.put("id", this.id);
        JsonObject.put("name", this.name);
        JsonObject.put("value", this.value);
        JsonObject.put("comments", this.comments);
        JsonObject.put("createdBy", this.createdBy);
        JsonObject.put("createTime", this.createTime);
        JsonObject.put("updatedBy", this.updatedBy);
        JsonObject.put("updateTime", this.updateTime);
        return JsonObject;
    }
}